/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Haciendo click en el boton las lineas se dibujan y haciendo click en la lisa se muestra
 * por consola si hay una interseccion o no.
 *
 * 
 */
package pruebaColiciones;

import tsb.arrastrar.*;
import java.awt.*;
import java.util.LinkedList;

import java.awt.Toolkit;
import java.awt.geom.Line2D;
import java.util.Iterator;
import java.util.Vector;

public class PanelColiciones extends javax.swing.JPanel {
    private Graphics gc;
     private LinkedList<Line2D.Double> listaLineas = new LinkedList<Line2D.Double>();
     
      Point ini=new Point(10,10);
        Point fin=new Point(100,100);
         Line2D.Double linea = new Line2D.Double( ini.x, ini.y, fin.x, fin.y );

    public PanelColiciones() {
        initComponents();
        
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(291, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(159, 159, 159))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(jButton1)
                .addContainerGap(353, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        gc=getGraphics();
        gc.setXORMode(getBackground());
       
        Graphics2D g2d = ( Graphics2D ) gc;
        g2d.setPaint( new Color(0, 255, 0));//verde
         float guiones[] = { 1 };
        g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, guiones, 0 ) );
       
        g2d.draw(linea );
        this.listaLineas.add(linea);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
//        System.out.println(evt);
//        Line2D.Double l = new Line2D.Double( evt.getX(), evt.getY(),evt.getX()+50, evt.getY()+50);
//        
//        for(Line2D.Double linea: this.listaLineas)
//        {
//            if(linea.intersectsLine(l))
//            {
//                System.out.println("#####Inserseccion");
//            }
//        
//        }
        
        
         gc=getGraphics();
        gc.setXORMode(getBackground());
       
        Graphics2D g2d = ( Graphics2D ) gc;
        g2d.setPaint( new Color(255, 255, 255));//verde
         float guiones[] = { 1 };
        g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, guiones, 0 ) );
        Line2D.Double linea2 = new Line2D.Double( evt.getX(),evt.getY(),evt.getX()+5,evt.getY()-5);
         g2d.draw(linea2 );
         
         
         if(linea2.intersectsLine(linea))
         {
             System.out.println("#########Interseccion");
         }
         else
         {
             System.out.println("No hay inserccion");
         }
        
//        Line2D.Double l1 = new Line2D.Double(10,10,100,100);
//        Line2D.Double l2 = new Line2D.Double(10,100,100,10);
//        if(l1.intersectsLine(l2))
//        {
//            System.out.println("Interseccion");
//        }
        
        
        
       
        
    }//GEN-LAST:event_formMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables


 @Override
    public void paintComponent(Graphics g){
     
        super.paintComponent(g);
        for(Line2D.Double linea: this.listaLineas)
        {
                Graphics2D g2d = ( Graphics2D ) gc;
        g2d.setPaint( new Color(0, 255, 0));//verde
         float guiones[] = { 1 };
        g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, guiones, 0 ) );
        g2d.draw(linea );
            
            
        }
    }

    
   
    
}

